; (function () {
  window.rbacVue = new Vue({
    el: '#rbac',
    data: {
      pid: 0,
      index: 0,
      memberType: 'A',
      memberData: [],
      memberActive: '',
      odata: [],
      adata: [],
      csid: null,
      adminData: [],
      personData: [],
      ids: {
        memberId: null,
        headId: null
      },
      isShowAuthFlag: false,
      allProjects: [],
      projectflag: false,
      allcheckProjectflag: false,
      partProject: [],
      getprojectids: [],
      status: '', // 0代表负责人    1代表成员
      permissions: JSON.parse(window.localStorage.getItem('permissions')),

      activeAdmin: false,
      activeMember: false,

      cusAdminId: '',
      cusMemberId: '',
      cusPid:''
    },
    computed: {
      isshowMark() {
        return authConfig.isShowAuthFlag
      },
      treeList() {
        let arr = [];
        let parent, children;
        parent = this.adata
        children = this.adata
        let lindex = 0;
        for (let i = parent.length - 1; i >= 0; i--) {
          let item0 = parent[i];
          for (let j = 0; j < children.length; j++) {
            let item1 = children[j];
            if (item0.id == item1.parentId) {
              item0.children.push(item1)
            }
          }
        }
        for (let k = 0; k < parent.length; k++) {
          if (Number(parent[k].parentId) > Number(this.csid)) {
            parent.splice(k, 1)
            k--
          }
        }
        arr = parent

        let count = 0;
        guierzi(arr, count)
        return arr
      }
    },
    methods: {
      // 获取客户数据数据
      orgDetail(id, type) {
        hlhttp.get({
          url: 'organization',
          data: {
            organization_id: id,
          },
          success: res => {
            this.cusPid = res.pid
            if (type == 'admin') {
              this.activeAdmin = res.customerDataIsolationFlag ? (res.customerDataIsolationFlag == 'Y' ? true : false) : false
              this.cusAdminId = id
            } else {
              this.activeMember = res.customerDataIsolationFlag ? (res.customerDataIsolationFlag == 'Y' ? true : false) : false
              this.cusMemberId = id
            }
          }
        });
      },
      // 客户数据开关
      switchCustomer(obj, type) {
        let flag = true
        if (type == 'admin') {
          flag = obj ? 'Y' : 'N'
          this.activeAdmin = obj
        } else {
          flag = obj ? 'Y' : 'N'
          this.activeMember = obj
        }
        hlhttp.put({
          url: 'organization/customer_data_isolation/_update',
          data: {
            organization_id: type == 'admin' ? this.cusAdminId : this.cusMemberId,
            pid: this.cusPid,
            customer_data_isolation_flag: flag
          },
          success: res => {
            if (type == 'admin') {
              this.orgDetail(this.cusAdminId, 'admin')
            } else {
              this.orgDetail(this.cusMemberId, 'member')
            }
            hl.toast('设置成功')
          }
        });
      },
      //高度设置
      setHeight() {
        var h = document.querySelector('.cm-content').scrollHeight;
        if (document.querySelector('.left-tissue')) {
          document.querySelector('.left-tissue').style.height = h + 'px';
        }
      },
      //获取身份信息
      getStwxdd() {
        hlhttp.get({
          url: 'user_sub_organizations',
          data: { pid: this.pid, member_type: this.memberType, show_member: 'N' },
          success: (res) => {
            if (!res.subOrgs.length) return;
            res.subOrgs.map(item => {
              if (item.orgType == 'WX') {
                item.index = 0
              }
              if (item.orgType == 'DD') {
                item.index = 1
              }
            })
            res.subOrgs = res.subOrgs.sort(hlArrayUtil.compareAvg('index'))
            this.memberActive = res.subOrgs[0].orgType
            if (res.subOrgs.length == 1) {
              res.subOrgs[0].shortName = '组织架构'
            }
            this.pid = res.subOrgs[0].id
            this.csid = res.subOrgs[0].id
            this.memberData = res.subOrgs;

            this.getStwxddData()
          }
        })
      },
      //根据身份信息id调用
      getStwxddData(fn) {
        hlhttp.get({
          url: 'user_sub_organizations',
          data: { pid: this.pid, member_type: this.memberType, show_member: 'N' },
          success: (data) => {
            data.subOrgs.map(item => {
              item.parentId = this.pid;
              item.children = [];
              this.odata.push(item)
            })
            this.odata = hlArrayUtil.removeRepeat(this.odata, 'id');
            this.odata = this.odata.sort(hlArrayUtil.compareAvg('id'))
            this.adata = JSON.parse(JSON.stringify(this.odata))
            if (fn) fn();
          }
        })
      },
      getPidData(ops) {
        if (ops.id == this.pid) return
        this.pid = ops.id;
        this.getStwxddData()
        this.ids = { memberId: ops.memberOrgId, headId: ops.adminOrgId }
        this.getpersonData()
      },
      //切换导航
      tabNav(item) {
        if (this.memberData.length == 1) return;
        this.memberActive = item.orgType;
        this.pid = item.id;
        this.csid = item.id;
        this.odata.splice(0, this.odata.length)
        this.adata.splice(0, this.adata.length)
        this.personData = []
        this.getStwxddData()
      },
      //获取组织下人员配置列表 – 权限配置
      getpersonData() {
        hlhttp.get({
          url: 'organization/member_configure',
          data: { org_head_id: this.ids.headId, org_member_id: this.ids.memberId },
          success: (data) => {
            data.orgAdmin.map(item => {
              item.flag = false
            })
            data.orgMember.map(item => {
              item.flag = false
            })
            this.adminData = data.orgAdmin
            this.personData = data.orgMember
            this.orgDetail(this.ids.headId,'admin')
            this.orgDetail(this.ids.memberId,'member')
          }
        })
      },
      //成员类型设置接口
      setperson(item) {
        let type = item.orgUserTypeCode == "ADMIN" ? 'Y' : 'N'
        hlhttp.put({
          url: 'organization/set_member_type',
          data: { org_user_id: item.orgUserId, org_head_id: this.ids.headId, org_member_id: this.ids.memberId, is_only_member: type },
          success: () => {
            hl.toast('设置成员成功')
            this.getpersonData()
          }
        })
      },
      //选中负责人
      selectEdAdmin(item) {
        this.adminData.map(item => {
          item.flag = false
        })
        item.flag = !item.flag
      },
      //负责人授权
      setAuthAdmin() {
        if (!this.ids.memberId) {
          hl.toast('请先选中组织')
          return
        }
        authConfig.isShowAuthFlag = true;
        authConfig.positionId = this.ids.headId;
        authConfig.getFirstMenuData()
        authConfig.getPositionPrivilege()
      },
      //成员授权
      setAuthMember() {
        if (!this.ids.memberId) {
          hl.toast('请先选中组织')
          return
        }
        authConfig.isShowAuthFlag = true;
        authConfig.positionId = this.ids.memberId;
        authConfig.getFirstMenuData()
        authConfig.getPositionPrivilege()
      },
      //负责人授权项目
      setProjectAdmin(id) {
        hlhttp.get({
          url: 'organization/projects',
          data: { position_id: id },
          success: (data) => {
            data.map(item => {
              item.flag = false
            })
            data.map(item => {
              this.partProject.map(item0 => {
                if (item.id == item0.id) {
                  item.flag = true
                }
              })
            })
            this.allProjects = data;
            this.projectflag = true;
          }
        })
      },
      getTopId() {
        hlhttp.get({
          url: 'sub_organizations',
          data: { pid: 0 },
          success: (data) => {
            this.setProjectAdmin(data[0].id)
          }
        })
      },
      //获取负责人项目
      getAdminProject() {
        this.status = '0'
        if (!this.ids.memberId) {
          hl.toast('请先选中组织')
          return
        }
        hlhttp.get({
          url: 'position/projects',
          data: { position_id: this.ids.headId },
          success: (data) => {
            this.partProject = data
            this.getTopId()
          }
        })
      },
      //获取成员项目
      getMemberProject() {
        this.status = '1'
        if (!this.ids.memberId) {
          hl.toast('请先选中组织')
          return
        }
        hlhttp.get({
          url: 'position/projects',
          data: { position_id: this.ids.memberId },
          success: (data) => {
            this.partProject = data
            this.getTopId()
          }
        })
      },
      //全选不选项目
      allcheckProject() {
        this.allcheckProjectflag = !this.allcheckProjectflag;
        if (this.allcheckProjectflag) {
          this.allProjects.map(item => {
            item.flag = true
          })
          this.partProject = JSON.parse(JSON.stringify(this.allProjects))
        }
        if (!this.allcheckProjectflag) {
          this.allProjects.map(item => {
            item.flag = false
            this.partProject = []
          })
        }
      },
      //单选操作
      singleCheck(i) {
        if (this.allProjects[i].flag) {
          this.allProjects[i].flag = false;
          this.partProject.map((item, index) => {
            if (item.id == this.allProjects[i].id) {
              this.partProject.splice(index, 1)
            }
          })
          this.allcheckProjectflag = false
          return
        }
        if (!this.allProjects[i].flag) {
          this.allProjects[i].flag = true;
          this.partProject.push(this.allProjects[i])
        }
      },
      //删除操作
      delCheck(i) {
        this.allProjects.map(item => {
          if (item.id == this.partProject[i].id) {
            item.flag = false
          }
        })
        this.partProject.splice(i, 1)
        this.allcheckProjectflag = false
      },
      //保存负责人项目
      saveAdminProject() {
        this.getprojectids = []
        this.partProject.map(item => {
          this.getprojectids.push(item.id)
        })
        hlhttp.post({
          url: 'assign_project_to_organization',
          data: { position_id: this.ids.headId, project_ids: this.getprojectids.toString() || '#' },
          success: () => {
            hl.toast('保存成功')
            this.projectflag = false
            this.allcheckProjectflag = false
          }
        })
      },
      //保存成员项目
      saveMemberProject() {
        this.getprojectids = []
        this.partProject.map(item => {
          this.getprojectids.push(item.id)
        })
        hlhttp.post({
          url: 'assign_project_to_organization',
          data: { position_id: this.ids.memberId, project_ids: this.getprojectids.toString() || '#' },
          success: () => {
            hl.toast('保存成功')
            this.projectflag = false
            this.allcheckProjectflag = false
          }
        })
      }
    },
    created() {
      this.setHeight()
    },
    mounted() {
      this.getStwxdd()
      window.onresize = () => {
        this.setHeight()
      }
    }
  })

  function guierzi(arr, count) {
    count++
    arr.map(item => {
      item.lindex = count
      if (item.children.length) {
        guierzi(item.children, count)
      }
    })
  }
}())
